home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / OpenTransport / Open Transport 1.0.5b4 / Open Transport SDK / Open Tpt Module Developer / Includes / OpenTptModule.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-24  |  25.1 KB  |  905 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OpenTptModule.h
  3.  
  4.     Contains:    headers for STREAM modules developement
  5.  
  6.     Copyright:    © 1993-1995 by Apple Computer, Inc. and Mentat Inc., all rights reserved.
  7.  
  8.  
  9. */
  10.  
  11. #ifndef __OPENTPTMODULE__
  12. #define __OPENTPTMODULE__
  13.  
  14. #define OTKERNEL    1
  15.  
  16. #ifndef __OPENTRANSPORT__
  17. #include <OpenTransport.h>
  18. #endif
  19. /*
  20.  * Some typedefs needed to include cred.h and mistream.h
  21.  */
  22. typedef UInt32    uint;        // this line deleted from release disk
  23. typedef UInt16    ushort;        // this line deleted from release disk
  24. typedef UInt32    ulong;        // this line deleted from release disk
  25. typedef UInt32    uid_t;
  26. typedef UInt32    gid_t;
  27. typedef UInt32    dev_t;
  28.  
  29. #ifndef _CRED_
  30. #include <cred.h>
  31. #endif
  32. #ifndef __OPENTPTDEV__
  33. #include <OpenTptDev.h>
  34. #endif
  35. #ifndef _STRLOG_
  36. #include <strlog.h>
  37. #endif
  38.  
  39. #ifndef __MEMORY__
  40. #include <Memory.h>
  41. #endif
  42.  
  43. #if defined(__MWERKS__) && GENERATING68K
  44. #pragma pointers_in_D0
  45. #endif
  46.  
  47. /*******************************************************************************
  48. ** Some IOCTL constants modules need to know
  49. ********************************************************************************/
  50.  
  51. #define I_LINK            MIOC_CMD(MIOC_STREAMIO,12)        /* connect stream under mux fd */
  52. #define I_UNLINK        MIOC_CMD(MIOC_STREAMIO,13)        /* disconnect two streams */
  53. #define I_PLINK         MIOC_CMD(MIOC_STREAMIO,29)        /* permanently connect a stream under a mux */
  54. #define I_PUNLINK        MIOC_CMD(MIOC_STREAMIO,30)        /* disconnect a permanent link */
  55.  
  56. /*******************************************************************************
  57. ** Printing functions
  58. ********************************************************************************/
  59.  
  60. enum
  61. {
  62.     kOTPrintOnly        = 0,
  63.     kOTPrintThenStop    = 1
  64. };
  65.  
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69.  
  70. int OTKernelPrintf(int toDo, char* fmt, ...);
  71.  
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75.  
  76. /*******************************************************************************
  77. ** Function to convert the "long" value that comes back in some of the
  78. ** netbufs as a result code to the equivalent OSStatus
  79. ********************************************************************************/
  80.  
  81. typedef long                    OTError;
  82.  
  83. #define GetEError(v)            ((OTUnixErr)(((v) >> 16) & 0xffff))
  84. #define GetXTIError(v)            ((OTXTIErr)((v) & 0xffff))
  85. #define MakeEError(e)            ((OTError)(((((UInt32)(e)) << 16) | TSYSERR)))
  86. #define MakeXTIError(xti)        ((OTError)(xti))
  87.  
  88. #ifdef __cplusplus
  89. extern "C" {
  90. #endif
  91.  
  92. OSStatus OTErrorToOSStatus(OTError);
  93.                     
  94. #ifdef __cplusplus
  95. }
  96. #endif
  97.  
  98. /*******************************************************************************
  99. ** Things need to be able to export a module
  100. ********************************************************************************/
  101.  
  102. /*    -------------------------------------------------------------------------
  103.     Synchronization level codes.  These are supplied to modsw_install and
  104.     stored in the appropriate tables.  sth_osr_open and
  105.     sth_ipush use these to set up synch queue subordination for new devices
  106.     and modules.
  107.     ------------------------------------------------------------------------- */
  108.  
  109.     enum
  110.     {
  111.         SQLVL_QUEUE            = 1,
  112.         SQLVL_QUEUEPAIR        = 2,
  113.         SQLVL_MODULE        = 3,
  114.         SQLVL_GLOBAL        = 4,
  115.     
  116.         SQLVL_DEFAULT        = SQLVL_MODULE
  117.     };
  118.  
  119. /*    -------------------------------------------------------------------------
  120.     The install_info structure.
  121.     ------------------------------------------------------------------------- */
  122.  
  123.     struct install_info
  124.     {
  125.         struct streamtab*    install_str;     /* Streamtab pointer.        */
  126.         UInt32                 install_flags;
  127.         UInt32                 install_sqlvl;    /* Synchronization level.    */
  128.         char*                install_buddy;    /* Shared writer list buddy */
  129.         long                ref_load;        /* Set to 0                    */
  130.         UInt32                ref_count;        /* set to 0                    */
  131.     };
  132.     
  133.     typedef struct install_info    install_info;
  134.     
  135.     //
  136.     //    Flags used in the install_flags field
  137.     //
  138.     enum
  139.     {
  140.          kOTModIsDriver            = 0x00000001,
  141.          kOTModIsModule            = 0x00000002,
  142.          
  143.          kOTModUpperIsTPI        = 0x00001000,
  144.          kOTModUpperIsDLPI        = 0x00002000,
  145.          kOTModLowerIsTPI        = 0x00004000,
  146.          kOTModLowerIsDLPI        = 0x00008000,
  147.         //
  148.         // These flags are only valid if kOTModIsDriver is set.
  149.         //
  150.         kOTModIsLowerMux        = 0x10000000,
  151.         kOTModIsComplexDriver    = 0x20000000,
  152.         //
  153.         // These flags are only valid if kOTModIsModule is set.
  154.         //
  155.         kOTModIsFilter            = 0x40000000,
  156.         kOTModPushAlways        = 0x80000000
  157.     };
  158.  
  159. /*    -------------------------------------------------------------------------
  160.     Typedef for the GetOTInstallInfo function
  161.     
  162.     Your module must export this function, and return a pointer to the
  163.     install_info structure for the module.
  164.     ------------------------------------------------------------------------- */
  165.  
  166.     typedef install_info* (* _CDECL GetOTInstallInfoProcPtr)(void);
  167.  
  168. /*    -------------------------------------------------------------------------
  169.     Typedef for the InitStreamModule function
  170.     
  171.     Your module can optionally export this function.  It will be called 
  172.     whenever your module is about to be loaded into a stream for the
  173.     first time, or if it is about to be reloaded after having been 
  174.     unloaded. Return false if your module should NOT be loaded.
  175.     For STREAMS modules, the void* parameter will be NULL.  For drivers, it
  176.     will be the same cookie parameter that was used for registering the module.
  177.     For PCI card drivers, this will be a pointer to the OTPCIInfo structure,
  178.     which can also be interpreted as a RegEntryIDPtr.
  179.     ------------------------------------------------------------------------- */
  180.     
  181.     typedef Boolean (* _CDECL InitStreamModuleProcPtr)(void*);
  182.  
  183. /*    -------------------------------------------------------------------------
  184.     Typedef for the TerminateStreamModule function
  185.     
  186.     Your module can optionally export this function.  It will be called 
  187.     whenever your module has been closed for the last time (i.e. no other 
  188.     outstanding instances of the module exist).
  189.     ------------------------------------------------------------------------- */
  190.     
  191.     typedef void (* _CDECL TerminateStreamModuleProcPtr)(void*);
  192.  
  193. /*******************************************************************************
  194. ** Equates for shared library prefixes
  195. ********************************************************************************/
  196.  
  197.     /*
  198.      * Prefixes for Open Transport driver experts.
  199.      */
  200.     #define kOTPortScannerPrefix        "ot:pScnr$"
  201.     
  202.     /*
  203.      * Interface ID for "C" STREAMS Modules for ASLM
  204.      */
  205.     #define kOTModuleInterfaceID        kOTModulePrefix "CStrmMod"
  206.  
  207. /*******************************************************************************
  208. ** Functions for dealing with major and minor device numbers
  209. ********************************************************************************/
  210.  
  211.     typedef UInt16            major_t;
  212.     typedef UInt16            minor_t;
  213.     
  214.     /* major part of a device */
  215.     #define getmajor(x)         ((major_t)(((unsigned long)(x)>>16)&0xffff))
  216.     
  217.     /* minor part of a device */
  218.     #define getminor(x)         ((minor_t)((x)&0xffff))
  219.     
  220.     /* make a device number */
  221.     #define makedev(x,y)        ((dev_t)((((dev_t)(x))<<16) | ((y) & 0xffff)))
  222.     
  223.     #define getemajor            getmajor
  224.     #define geteminor            getminor
  225.     #define makedevice            makedev
  226.     
  227.     #define    etoimajor(majnum)    (majnum)
  228.     #define    itoemajor(majnum,j)    (majnum)
  229.  
  230. /*******************************************************************************
  231. ** Some definitions
  232. ********************************************************************************/
  233.  
  234.     /*
  235.      * The first minor number that you should use for CLONEOPENs.
  236.      * Minor numbers 0 through 9 are reserved for use by the modules
  237.      * for various control streams.
  238.      */
  239.      enum
  240.      {
  241.          kFirstMinorNumber    = 10
  242.     };
  243.  
  244.     /*
  245.      * Extra OTCommand codes that may appear on your module queue.
  246.      * These are extensions to the TPI specification for Open Transport.
  247.      * T_PRIVATE_REQ is the first available TPI message number for private
  248.      * use by modules (assuming you don't want to be confused by standard
  249.      * TPI messages).
  250.      */
  251.     enum
  252.     {
  253.         T_TIMER_REQ        = 80,    // Timer event
  254.         T_MIB_REQ        = 81,    // Request module's MIB
  255.         T_MIB_ACK        = 82,    // The module's MIB is available
  256.         
  257.         T_PRIVATE_REQ    = 90    // The first private request available
  258.     };
  259.  
  260. /*******************************************************************************
  261. ** Logging Macros
  262. ********************************************************************************/
  263.  
  264. #ifdef __cplusplus
  265. extern "C" {
  266. #endif
  267.  
  268. void OTStrlog(queue_t*, int, int, const char*);
  269.  
  270. #ifdef __cplusplus
  271. }
  272. #endif
  273. //
  274. // These are enums for the level value
  275. //
  276. enum
  277. {
  278.     kOTBug                    = 0,        // Use this for a bug in OT
  279.     kOTClientBug            = 1,        // Use this for a client bug (client did bad thing!)
  280.     kOTUnrecoverableProblem    = 2,
  281.     kOTRecoverableProblem    = 3,
  282.     kOTPacketProblem        = 4,
  283.     kOTInfoMessage            = 5
  284. };
  285.  
  286. #define STRLOG(q, lvl, flags, str)        OTStrlog(q, lvl, flags, str)
  287.  
  288. #if qDebug
  289.     #define STRLOG1(q, lvl, flags, str)    OTStrlog(q, lvl, flags, str)
  290. #else
  291.     #define STRLOG1(q, lvl, flags, str)
  292. #endif
  293.  
  294. #if qDebug > 1
  295.     #define STRLOG2(q, lvl, flags, str)    OTStrlog(q, lvl, flags, str)
  296. #else
  297.     #define STRLOG2(q, lvl, flags, str)
  298. #endif
  299.  
  300. #if qDebug > 2
  301.     #define STRLOG3(q, lvl, flags, str)    OTStrlog(q, lvl, flags, str)
  302. #else
  303.     #define STRLOG3(q, lvl, flags, str)
  304. #endif
  305.  
  306. /*******************************************************************************
  307. ** Structure definitions
  308. **
  309. ** These go along with the extra definitions above.
  310. ********************************************************************************/
  311.  
  312.     struct  T_MIB_req
  313.     {
  314.         long    PRIM_type;        /* Always T_MIB_REQ */
  315.     };
  316.     
  317.     struct  T_MIB_ack
  318.     {
  319.         long    PRIM_type;        /* Always T_MIB_ACK     */
  320.         long    MIB_length;        /* MIB length             */        
  321.         long    MIB_offset;        /* MIB Offset            */        
  322.     };
  323.     
  324.     struct T_stream_timer
  325.     {
  326.         long        PRIM_type;    /* Always T_TIMER_REQ */
  327.         union
  328.         {
  329.             long    USER_long;
  330.             void*    USER_ptr;
  331.     #ifndef __cplusplus
  332.         }            USER_data;
  333.     #else
  334.         };
  335.     #endif
  336.     };
  337.  
  338. /*    -------------------------------------------------------------------------
  339.     Internal port record structures
  340.     ------------------------------------------------------------------------- */
  341.  
  342.     /*    
  343.      * One TPortRecord is created for each instance of a port.
  344.      * For Instance 'enet' identifies an ethernet port.
  345.      * An TPortRecord for each ethernet card it finds, with an
  346.      * OTPortRef that will uniquely allow the ethernet driver to determine which
  347.      * port it is supposed to open on.
  348.      */
  349.  
  350.     typedef struct TPortRecord    TPortRecord;
  351.  
  352.     struct TPortRecord
  353.     {
  354.         OTLink                    fLink;
  355.         char*                    fPortName;
  356.         char*                    fModuleName;
  357.         char*                    fResourceInfo;
  358.         char*                    fSlotID;
  359.         struct TPortRecord*        fAlias;
  360.         size_t                    fNumChildren;
  361.         OTPortRef*                fChildPorts;
  362.         UInt32                    fPortFlags;
  363.         UInt32                    fInfoFlags;
  364.         UInt32                    fCapabilities;
  365.         OTPortRef                fRef;
  366.         struct streamtab*        fStreamtab;
  367.         void*                    fContext;
  368.         void*                    fExtra;
  369.     };
  370.  
  371. /*******************************************************************************
  372. ** "C" Interface routines
  373. ********************************************************************************/
  374.  
  375. #ifdef __cplusplus
  376. extern "C" {
  377. #endif
  378.  
  379. /*    -------------------------------------------------------------------------
  380.     Interrupt control functions
  381.     ------------------------------------------------------------------------- */
  382.  
  383. #if USES68KINLINES
  384.     //
  385.     // MPS_INTR_STATE saves the current interrupt state
  386.     // Its definition undoubtably changes with the definition
  387.     // of mps intr enable/disable, so this typedef is
  388.     // also inside the USE68KINLINES conditional.
  389.     //
  390.     typedef UInt8    MPS_INTR_STATE;
  391.     //
  392.     // Disable interrupts and save the state
  393.     //
  394.     #pragma parameter mps_intr_disable(__A0)
  395.     void mps_intr_disable(MPS_INTR_STATE*) =
  396.     {
  397.         0x40C0,             /*        move    sr,d0        */
  398.         0xE040,                /*        asr.w    #8,d0        */
  399.         0x007C, 0x0600,        /*        ori        #$600,sr    */
  400.         0x027C, 0xFEFF,     /*        andi    #$FEFF,sr    */
  401.         0x1080                /*         move.b d0,(a0)         */
  402.     #if qDebug > 1
  403.         , 0x0200, 0x0070,    /*        andi.b    #$70,d0        */
  404.         0x0c00, 0x0070,        /*        cmpi.b    #$70,d0        */
  405.         0x6602,                /*        bne.s    @3            */
  406.         0xA9FF                /*        DebugBreak            */
  407.     #endif
  408.     };
  409.     //
  410.     // Enable interrupts from the saved state
  411.     //
  412.     #pragma parameter mps_intr_enable(__A0)
  413.     void mps_intr_enable(MPS_INTR_STATE* ptr) =
  414.     {
  415.     #if qDebug > 1
  416.         0x40C0,             /*        move    sr,d0        */
  417.         0x0240, 0x0700,        /*        andi.w    #$700,d0    */
  418.         0x0C40, 0x0600,        /*        cmpi.w    #$600,d0    */
  419.         0x6702,                /*         beq.s    @2             */
  420.         0xA9FF,                /*         DebugBreak             */
  421.     #endif
  422.         0x1010,                /* @2    move.b    (a0),d0        */
  423.     #if qDebug > 1
  424.         0x0200, 0x0070,        /*        andi.b    #$70,d0        */
  425.         0x0c00, 0x0070,        /*        cmpi.b    #$70,d0        */
  426.         0x6602,                /*        bne.s    @3            */
  427.         0xA9FF,                /*        DebugBreak            */
  428.         0x1010,                /* @3    move.b    (a0),d0        */
  429.     #endif
  430.         0xE140,                /*        asl.w    #8,d0        */
  431.         0x46C0                /*        move    d0,sr         */
  432.     };
  433. #else
  434. #if TRADMACOS
  435.     typedef UInt8    MPS_INTR_STATE;
  436. #else
  437.     typedef void*    MPS_INTR_STATE;
  438. #endif
  439.  
  440.     void mps_intr_disable(MPS_INTR_STATE*);
  441.     void mps_intr_enable(MPS_INTR_STATE*);
  442. #endif
  443.  
  444.  
  445. /*    -------------------------------------------------------------------------
  446.     The kOTPortScannerInterfaceID define is what you need to add to your
  447.     export file for the "interfaceID = " clause.  
  448.     PortScanProcPtr is the typedef for the scanning function.
  449.     Your port-scanning function must be exported by the name "OTScanPorts".
  450.     Your port-scanning function set must use the prefix kOTPortScannerPrefix.
  451.     ------------------------------------------------------------------------- */
  452.  
  453.     #define kOTPortScannerInterfaceID            kOTKernelPrefix "pScnr,1.0"
  454.     #define kOTPseudoPortScannerInterfaceID        kOTKernelPrefix "ppScnr,1.0"
  455.     
  456.     typedef void (* _CDECL PortScanProcPtr)(UInt32 scanType);
  457.  
  458.     enum    /* scanType */
  459.     {
  460.         kOTInitialScan        = 0,
  461.         kOTScanAfterSleep    = 1
  462.     };
  463.             
  464. /*    -------------------------------------------------------------------------
  465.     Port Registration
  466.     
  467.     These routines can be used to register, find and iterate through the
  468.     various ports on the machine
  469.     ------------------------------------------------------------------------- */
  470.         //
  471.         // Register a port. The name the port was registered under is returned in
  472.         // the fPortName field.
  473.         //
  474.     OSStatus    OTRegisterPort(OTPortRecord* portInfo, void* ref);
  475.         //
  476.         // Unregister the port with the given name (If you re-register the
  477.         // port, it may get a different name - use OTChangePortState if
  478.         // that is not desireable).  Since a single OTPortRef can be registered
  479.         // with several names, the API needs to use the portName rather than
  480.         // the OTPortRef to disambiguate.
  481.         //
  482.     OSStatus        OTUnregisterPort(const char* portName, void**);
  483.         //
  484.         // Change the status of the port. 
  485.         //
  486.     void            OTChangePortState(OTPortRef ref, UInt32 theChange, OSStatus why);
  487.         //
  488.         // Find the TPortRecord for a given Port Name
  489.         //
  490.     TPortRecord*    OTFindPort(const char* portName);
  491.         //
  492.         // Find the "nth" TPortRecord
  493.         //
  494.     TPortRecord*    OTGetIndexedPort(size_t index);
  495.         //
  496.         // Find another port that is active and conflicts with
  497.         // the port described by "ref"
  498.         //
  499.     TPortRecord*    OTFindPortConflict(OTPortRef ref);
  500.         //
  501.         // Other ways of finding the port
  502.         //
  503.     TPortRecord*    OTFindPortByRef(OTPortRef);
  504.     TPortRecord*    OTFindPortByDev(dev_t);
  505.  
  506. /*    -------------------------------------------------------------------------
  507.     Timer functions
  508.     ------------------------------------------------------------------------- */
  509.  
  510.     mblk_t*            mi_timer_q_switch(mblk_t*, queue_t*, mblk_t*);
  511.     Boolean            mi_timer_cancel(mblk_t*);
  512.     void            mi_timer(mblk_t*, unsigned long);
  513.     mblk_t*            mi_timer_alloc(queue_t*, size_t);
  514.     void            mi_timer_free(mblk_t*);
  515.     Boolean            mi_timer_valid(mblk_t*);
  516.  
  517. /*    -------------------------------------------------------------------------
  518.     Miscellaneous helpful routines
  519.     ------------------------------------------------------------------------- */
  520.     /*
  521.      * This is the typedef for a function that will be called when a message
  522.      * created by OTAllocMsg is destroyed.
  523.      */
  524.     typedef void    (* _CDECL EsbFreeProcPtr)(char* arg);
  525.     
  526.     /*
  527.      * This function creates a message which points to "size" bytes of data
  528.      * at "buf".  When the message is freed, the EsbFreeProcPtr function "func"
  529.      * will be called with the argument "arg".
  530.      * NOTE: This function allows users of your buffer to modify the buffer.
  531.      */
  532.     mblk_t* OTAllocMsg(void* buf, size_t size,  EsbFreeProcPtr func, void* arg);
  533.  
  534.     /*
  535.      * Routines to allocate and free memory in your modules (these are
  536.      * interrupt-time safe!). 
  537.      */
  538.  
  539.     void*    OTAllocMem(size_t);
  540.     void    OTFreeMem(void*);
  541.     void*    OTReallocMem(void* ptr, size_t newSize);
  542.  
  543.     void*    OTAllocPortMem(size_t);
  544.     void    OTFreePortMem(void*);
  545.     /*
  546.      * Routines to calculate various sizes of STREAM messages
  547.      */
  548.     #define HEAD_SIZE(mp)    ((mp)->b_rptr - (mp)->b_datap->db_base)
  549.     #define TAIL_SIZE(mp)    ((mp)->b_datap->db_lim - (mp)->b_wptr)
  550.     #define MBLK_SIZE(mp)    ((mp)->b_wptr - (mp)->b_rptr)
  551.     #define DBLK_SIZE(mp)    ((mp)->b_datap->db_lim - (mp)->b_datap->db_base)
  552.     
  553.     #ifdef __cplusplus
  554.     extern "C" {
  555.     #endif
  556.     
  557.     int        mi_bcmp(const char* first, const char* second, size_t nBytes);
  558.     int        mi_sprintf (char * buf, char * fmt, ...);
  559.     
  560.     #ifdef __cplusplus
  561.     }
  562.     #endif
  563.     
  564.     #define bcopy(s, d, l)            OTMemcpy(d, s, l)
  565.     #define bzero(d, l)                OTMemzero(d, (size_t)(l))
  566.     #define bcmp(s, d, l)            mi_bcmp(s, d, l)
  567.  
  568.     /*
  569.      * Standard STREAMS bcopy, bzero, & bcmp take char* parameters.
  570.      * The BCOPY, BZERO, and BCMP routines take void* so that we do not have
  571.      * to cast all the pointers.
  572.      */
  573.     #define BCOPY(s, d, l)            bcopy((const char*)(s), (char*)(d), l)
  574.     #define BZERO(d, l)                bzero((char*)(d), l)
  575.     #define BCMP(s, d, l)            mi_bcmp((const char*)(s), (const char*)(d), l)
  576.  
  577.     /*
  578.      * Create sprintf and printf functions that will work in STREAM modules.
  579.      * Also, make sure that calling traditional "C" allocation routines
  580.      * will not compile.
  581.      */
  582.     #define sprintf                        mi_sprintf
  583.     #define printf                        OTKernelPrintf
  584.  
  585.     #define calloc                        (DONT_CALL*THIS_FUNCTION)
  586.     #define malloc                        (DONT_CALL*THIS_FUNCTION)
  587.     #define realloc                        (DONT_CALL*THIS_FUNCTION)
  588.     #define free                        (DONT_CALL*THIS_FUNCTION)
  589.  
  590.     /*
  591.      * A few other miscellaneous functions
  592.      */
  593.     int            drv_priv(struct cred* credp);
  594.     queue_t*    mi_allocq(struct streamtab*);
  595.     mblk_t*        mi_reallocb(mblk_t*, size_t new_Size);
  596.  
  597. #ifdef __cplusplus
  598. }
  599.  
  600. /*******************************************************************************
  601. ** Everything below here is C++ ONLY
  602. ********************************************************************************/
  603.  
  604. /*    -------------------------------------------------------------------------
  605.     TStreamQueue class
  606.     
  607.     This class is just a convenient interface to the queue structure
  608.     ------------------------------------------------------------------------- */
  609.  
  610.     class TStreamQueue : public queue
  611.     {
  612.         public:
  613.             void        EnableQueue();
  614.     };
  615.     
  616.     /*    -------------------------------------------------------------------------
  617.         Inline methods for TStreamQueue
  618.         ------------------------------------------------------------------------- */
  619.     
  620.         inline void TStreamQueue::EnableQueue()
  621.         {
  622.             if ( q_flag & QNOENB )
  623.             {
  624.                 enableok(this);
  625.                 if ( q_first )
  626.                     qenable(this);
  627.             }
  628.         }
  629.  
  630. /*    -------------------------------------------------------------------------
  631.     Class TStreamMessage
  632.     
  633.     This class is a C++ interface to the mblk_t structure defined in STREAMS
  634.     ------------------------------------------------------------------------- */
  635.     //
  636.     // The maximum stream buffer size is the largest "size_t" that
  637.     // doesn't look negative if interpreted as a signed number
  638.     //
  639.     enum
  640.     {
  641.         kMaxStreamBufferSize    = (((size_t)-1L) >> 1)
  642.     };
  643.  
  644. #if GENERATINGPOWERPC
  645. #pragma options align=mac68k
  646. #endif
  647.  
  648.     class TStreamMessage : public msgb
  649.     {
  650.         private:
  651.                 void*        operator new (size_t)    { return NULL; }
  652.                 
  653.         public:
  654.                 void*        operator new(size_t, size_t size)
  655.                     { return (TStreamMessage*)allocb(size, 0); }
  656.                 void*        operator new(size_t, void* buf, size_t size,
  657.                                          EsbFreeProcPtr func, void* arg)
  658.                     { return OTAllocMsg(buf, size, func, arg); }
  659.                 void        operator delete(void* ptr)
  660.                     { freemsg((mblk_t*)ptr); }
  661.     
  662.                     void            Reset(size_t = 0);
  663.                     void            ResetWithLeader(size_t);
  664.                     void            SetStartWithLeader(size_t);
  665.             
  666.                     void            FreeData();
  667.                     TStreamMessage*    RemoveData();
  668.                     void            AppendData(TStreamMessage* data);
  669.             
  670.                     size_t            GetSize() const;
  671.                     size_t            GetDataSize() const;
  672.                     size_t            GetMessageDataSize() const;
  673.                     void            SetDataSize(size_t);
  674.         
  675.                     void            SetType(UInt8);
  676.                     UInt8            GetType() const;
  677.                     TStreamMessage*    GetNextBlock() const;
  678.                     void            SetNextBlock(TStreamMessage*);
  679.                     char*            GetDataPointer() const;
  680.                     Boolean            IsReuseable(size_t size) const;
  681.             
  682.                     void            HideBytesAtFront(size_t);
  683.                     void            HideBytesAtEnd(size_t);
  684.             //
  685.             // The following functions ignore non-M_DATA blocks.
  686.             //
  687.                     const TStreamMessage*
  688.                                     _MDECL GetBlockAt(size_t& offset) const;
  689.                     const void*        _MDECL GetPointerTo(size_t offset, size_t* len) const;
  690.                     const void*        _MDECL GetPointerTo(size_t offset, size_t* len, void* bfr) const;
  691.             //
  692.             // This function makes a non-shared copy of any initial non-M_DATA block, and
  693.             // then makes shared copies of the requested data.  It only assumes 1 leading
  694.             // non-M_DATA block, but will skip any other non-M_DATA blocks while 
  695.             // searching for data to copy.
  696.             //
  697.                     TStreamMessage*    _MDECL MakeSharedCopy(size_t offset = 0, 
  698.                                                           size_t len = kMaxStreamBufferSize);
  699.  
  700.                     Boolean            _MDECL HasData() const;
  701.                     Boolean            _MDECL WriteData(void* buf, size_t len);
  702.             //
  703.             // These hide/remove the part of the data already read.  
  704.             //
  705.                     TStreamMessage*    _MDECL ReadData(void* buf, size_t* len);
  706.                     size_t            _MDECL ReadControl(void* buf, size_t len);
  707.     };
  708.     
  709. #if GENERATINGPOWERPC
  710. #pragma options align=reset
  711. #endif
  712.  
  713.     /*    -------------------------------------------------------------------------
  714.         Inline methods for TStreamMessage
  715.         ------------------------------------------------------------------------- */
  716.     
  717.         inline void TStreamMessage::Reset(size_t size)
  718.         {
  719.             b_rptr = b_datap->db_base;
  720.             b_wptr = b_rptr + size;
  721.         }
  722.         
  723.         inline void TStreamMessage::ResetWithLeader(size_t size)
  724.         {
  725.             b_wptr = b_datap->db_lim;
  726.             b_rptr = b_wptr - size;
  727.         }
  728.         
  729.         inline void TStreamMessage::SetStartWithLeader(size_t size)
  730.         {
  731.             b_rptr = b_datap->db_lim - size;
  732.         }
  733.         
  734.         inline void TStreamMessage::FreeData()
  735.         {
  736.             if ( b_cont )
  737.             {
  738.                 freemsg(b_cont);
  739.                 b_cont = NULL;
  740.             }
  741.         }
  742.         
  743.         inline TStreamMessage* TStreamMessage::RemoveData()
  744.         {
  745.             TStreamMessage* temp = (TStreamMessage*)b_cont;
  746.             b_cont = NULL;
  747.             return temp;
  748.         }
  749.         
  750.         inline void TStreamMessage::AppendData(TStreamMessage* mp)
  751.         {
  752.             linkb(this, mp);
  753.         }
  754.     
  755.         inline size_t TStreamMessage::GetSize() const
  756.         {
  757.             return b_datap->db_lim - b_datap->db_base;
  758.         }
  759.         
  760.         inline size_t TStreamMessage::GetDataSize() const
  761.         {
  762.             return b_wptr - b_rptr;
  763.         }
  764.         
  765.         inline size_t TStreamMessage::GetMessageDataSize() const
  766.         {
  767.             return msgdsize(this);
  768.         }
  769.     
  770.         inline void TStreamMessage::SetDataSize(size_t size)
  771.         {
  772.             b_wptr = b_rptr + size;
  773.         }
  774.     
  775.         inline void TStreamMessage::SetType(UInt8 type)
  776.         {
  777.             b_datap->db_type = type;
  778.         }
  779.     
  780.         inline unsigned char TStreamMessage::GetType() const
  781.         {
  782.             return b_datap->db_type;
  783.         }
  784.         
  785.         inline TStreamMessage* TStreamMessage::GetNextBlock() const
  786.         {
  787.             return (TStreamMessage*)b_cont;
  788.         }
  789.         
  790.         inline void TStreamMessage::SetNextBlock(TStreamMessage* mp)
  791.         {
  792.             b_cont = (mblk_t*)mp;
  793.         }
  794.         
  795.         inline char* TStreamMessage::GetDataPointer() const
  796.         {
  797.             return (char*)b_rptr;
  798.         }
  799.         
  800.         inline Boolean TStreamMessage::IsReuseable(size_t size) const
  801.         {
  802.             return (b_datap->db_ref == 1 && GetSize() >= size);
  803.         }
  804.         
  805.         inline void TStreamMessage::HideBytesAtFront(size_t len)
  806.         {
  807.             adjmsg(this, len);
  808.         }
  809.         
  810.         inline void TStreamMessage::HideBytesAtEnd(size_t len)
  811.         {
  812.             adjmsg(this, -len);
  813.         }
  814.  
  815. /*    -------------------------------------------------------------------------
  816.     TTimerMessage class
  817.     
  818.     This class implements an interface to the STREAM environment timer
  819.     facilities.  A TTimerMessage will be placed on the queue of your choice
  820.     when it's timer expires.
  821.     ------------------------------------------------------------------------- */
  822.  
  823. #if GENERATINGPOWERPC
  824. #pragma options align=mac68k
  825. #endif
  826.  
  827. #if GENERATINGPOWERPC
  828.     #define TIMER_BUG    1
  829. #else
  830.     #define TIMER_BUG    0
  831. #endif
  832.  
  833.     class TTimerMessage : public TStreamMessage
  834.     {
  835.     #if TIMER_BUG
  836.         private:
  837.     #else
  838.         public:
  839.     #endif
  840.             inline void* operator new(size_t, queue_t* q)
  841.                 {    return mi_timer_alloc(q, sizeof(T_stream_timer)); }
  842.             inline void* operator new(size_t, queue_t* q, size_t extra)
  843.                 {    return mi_timer_alloc(q, extra + sizeof(T_stream_timer)); }
  844.     
  845.             inline void operator delete(void* ptr)
  846.                 {    mi_timer_free((mblk_t*)ptr); }
  847.     
  848.         public:        
  849.                     Boolean            IsValid();
  850.                     TTimerMessage*    ChangeQueue(TStreamQueue* newQ, TTimerMessage* newMP);
  851.                     Boolean            Cancel();
  852.                     void            Schedule(OTTimeout time);
  853.     
  854.         private:
  855.             inline void* operator new(size_t)
  856.                 {    return 0; }
  857.     };
  858.     
  859.     inline TTimerMessage* NewTimerMsg(queue_t* q, size_t extra = 0)
  860.     {
  861.         return (TTimerMessage*)mi_timer_alloc(q, sizeof(T_stream_timer) + extra);
  862.     }
  863.     
  864.     inline void FreeTimerMsg(TTimerMessage* msg)
  865.     {
  866.         mi_timer_free(msg);
  867.     }
  868.     
  869. #if GENERATINGPOWERPC
  870. #pragma options align=reset
  871. #endif
  872.     
  873.     /*    -------------------------------------------------------------------------
  874.         Inline methods for TStreamTimer
  875.         ------------------------------------------------------------------------- */
  876.     
  877.         inline Boolean TTimerMessage::IsValid()
  878.         {
  879.             return mi_timer_valid(this);
  880.         }
  881.         
  882.         inline TTimerMessage* TTimerMessage::ChangeQueue(TStreamQueue* q, TTimerMessage* newMP)
  883.         {
  884.             return( (TTimerMessage*) mi_timer_q_switch(this, q, newMP));
  885.         }
  886.     
  887.         inline Boolean TTimerMessage::Cancel()
  888.         {
  889.             return mi_timer_cancel(this);
  890.         }
  891.         
  892.         inline void TTimerMessage::Schedule(OTTimeout time)
  893.         {
  894.             ((T_stream_timer*)b_rptr)->PRIM_type = T_TIMER_REQ;
  895.             mi_timer(this, (unsigned long)time);
  896.         }
  897.  
  898. #endif    /* __cplusplus    */
  899.  
  900. #if defined(__MWERKS__) && GENERATING68K
  901. #pragma pointers_in_A0
  902. #endif
  903.  
  904. #endif    /* __OPENTPTMODULE__    */
  905.